﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class ProductAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(true)
        {

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string imageurl;
        imageurl = upimage1();
        DataClassesDataContext db = new DataClassesDataContext();
        tb_BookInfo sp=new tb_BookInfo
        {
            BookName = TextBox1.Text.ToString(),
            BookID = int.Parse(TextBox2.Text),
            ClassID = int.Parse(DropDownList1.SelectedValue),
            BookUrl = imageurl,
            HotPrice = double.Parse(TextBox3.Text.ToString()),
            BookSyn = TextBox4.Text.ToString(),
            Recommend = CheckBox1.Checked,
            Sell_well = CheckBox2.Checked,
            Discount = CheckBox3.Checked
        };
        db.tb_BookInfo.InsertOnSubmit(sp);
        db.SubmitChanges();
        Reset();
        Response.Write("<script>alert('商品添加成功！')</script>");
    }
    public void Reset()
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        CheckBox1.Checked = false;
        CheckBox2.Checked = false;
        CheckBox3.Checked = false;
    }
    protected string upimage1()
    {
        Boolean fileOK = false;
        string fileNama="";
        //判断是否选中了文件
        if (FileUpload1.HasFile)
        {
            //获取上传文件扩展名
            String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            //限定上传文件类型
            String[] allowedExtensions = { ".gif", ".png", ".jpg", ".bmp", ".jpeg", ".psd" };
            //判断文件类型是否符合要求
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
            if (fileOK)
            {
                try
                {   //获取文件名称
                    string filepath = Server.MapPath("~/product_pic/") + FileUpload1.FileName;
                    fileNama = "~\\product_pic\\" + FileUpload1.FileName;
                    this.FileUpload1.PostedFile.SaveAs(filepath);
                   
                }
                catch (Exception ex)
                {
                    Response.Write("<script>alert('此文件格式不允许上传！！')</script>");
                    
                }
            }
        }
        return fileNama;

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Reset();
        
    }
}